const express = require('express');
const app = express();
const path = require('path');
const {router} = require('./server');
const cors = require('cors');
// 捕获全局异常  防止nodejs异常挂掉
process.on('unhandledRejection', (reason, p) => {
    console.log('Unhandled Rejection at: Promise', p, 'reason:', reason);
    // application specific logging, throwing an error, or other logic here
});

console.log('Listening on port 8080');
app.use(cors());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/public',express.static('./site/public'));
app.use('/',router);
app.all('*', function (req, res, next) {
    res.header('Access-Control-Allow-Origin', "*");
    res.header('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type');
    res.header('Access-Control-Allow-Methods', 'PUT, POST, GET, DELETE, OPTIONS');
    next();
});
const server = app.listen(8080, (error) => {
    if (error) {
        console.error(error);
    } else {
        console.info('==> Listening on port %s. Open up http://localhost:%s/ in your browser.', 8080, 8080);
    }
});
